app.push(function(){
    var TopMenu = function(){
    	this.init = function(){
            $.tmpl('navbar', {}).appendTo($('#body_header'));
            if(app.user_info.user_logined==true){
                app.main.ifData('onShopExist', app.user_info['user_shop'] && app.user_info['user_shop'].length);
                app.main.ifData('onArticleExist', app.user_info['user_article']>0);
                $('.navbar li[rel=forLogout], #loginForm').remove();
                $('.navbar li[rel=forLogin]').show();
                $('#logoutButton').click(function(){
                    mlhAPI.main.logout({
                        callback: function(data){
                            if(data.data.result!='error'){
                                location.reload();
                            }
                        }
                    });
                });
            }else{
                $('.navbar li[rel=forLogin]').remove();
                $('.navbar li[rel=forLogout]').show();
                //форма логина
                $('#loginButton').click(function(){
                    if(!$('#loginForm form').length){
                        $.tmpl('login_form', {}).prependTo($('#loginForm'));
                        $('#loginForm form').submit(function(){
                            var element = $(this).parent();
                            mlhAPI.main.login({
                                email: element.find('input[name=email]').attr('value'),
                                password: element.find('input[name=password]').attr('value'),
                                callback: function(data){
                                    console.log(data);
                                    if(data.data.result=='error'){
                                        element.parent().find('.alert[data-type=error]').addClass('alert-error').text(data.data.errors).show();
                                        element.parent().find('.alert-info').hide();
                                    }else{
                                        element.parent().find('.alert[data-type=error]').hide();
                                        element.parent().find('.alert-info').attr('class', 'alert alert-success').text('Вы успешно вошли в систему').show();
                                        setTimeout(function(){
                                            $('#loginForm').modal('hide');
                                            location.reload();
                                        }, 2000);
                                    }
                                }
                            });
                            return false;
                        });
                        $('#loginForm input').focus(function(){
                            $(this).parent().parent().find('.alert[data-type=error]').hide();
                            $(this).parent().parent().find('.alert-info').show();
                        });
                        $('#loginForm').modal('show');
                    } else {
                        $('#loginForm').modal('show');
                    }
                });
                app.main.bind('show_login_form',function(){$('#loginButton').click()});
            }
    	}
    	this.eraseBreadcrumbList = function(){
    		this.breadcrumbList = [['Главная',app.default_address]];
    	}
        this.breadcrumbList = [];
        this.buildBreadcrumb = function(){
            var el = $('ul.breadcrumb:first');
            el.html('');
            if(this.breadcrumbList.length<2){
                //return;
            }
            var buildEl = function(data,next){
                if(!data) return;
                var el = $('<li>');
                el.append($('<a href="'+data[1]+'" ' + (data[2] ? 'rel="' + data[2] + '" ' : '') + '></a>').text(data[0]));//.attr('class','active');
                if(next) el.append($('<span class="divider">/</span>'));
                return el;
            }
            for(var k=0; k<this.breadcrumbList.length; k++){
            	el.append(buildEl(this.breadcrumbList[k],this.breadcrumbList[k+1]));
            }
            el.show();
        }
        $('.navbar a.ajax').click(function(){
            app.main.ajaxObserver($(this).attr('href'));
            $('.navbar:first .nav li.open').removeClass('open');
            if($('.navbar:first .in').length){
            	$('.navbar:first .btn-navbar').click();
            }
            return false;
        });
        this.eraseBreadcrumbList();
    }
    app['topMenu'] = new TopMenu();
});